package org.gitman.agent.gui;

import java.awt.Color;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.gitman.agent.platform.Agent;
import org.gitman.agent.platform.PlatformManager;

public class AddAgentTableModel extends AbstractTableModel {

	private static String [] columns = {"", "Name", "Description"};
	private List<Agent> agents;
	private PlatformManager platformManager;
	private Boolean [] selectedTab;
	
	public AddAgentTableModel(PlatformManager platformManager){
		this.platformManager = platformManager;
		agents = platformManager.getAllAgents();
		selectedTab = new Boolean[agents.size()];
		for(int i =0;i<selectedTab.length;i++){
			selectedTab[i] = new Boolean(false);
		}
	}
	
	@Override
	public int getColumnCount() {
		return columns.length;
	}

	@Override
	public int getRowCount() {
		return agents.size();
	}
	
	@Override
	public String getColumnName(int arg0) {
		return columns[arg0];
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if(columnIndex == 0){
			return true;
		}
		return false;
	}
	
	@Override
	public Object getValueAt(int arg0, int arg1) {
		Agent agent = agents.get(arg0);
		switch (arg1) {
		case 0:
			return selectedTab[arg0];			
		case 1:
			return agent.getName();			
		case 2:
			return agent.getDescription();			
		default:
			return new String("###");
		}
	}
	
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if(selectedTab[rowIndex]){
			selectedTab[rowIndex] = false;
		}else{
			for(int i =0;i<selectedTab.length;i++){
				selectedTab[i] = false;
			}
			selectedTab[rowIndex] = !selectedTab[rowIndex];
		}
		fireTableDataChanged();
		super.setValueAt(aValue, rowIndex, columnIndex);
	}

	@Override
	public Class<?> getColumnClass(int arg0) {
		switch (arg0) {
		case 0:
			return Boolean.class;
		case 1:
			return String.class;
		case 2:
			return String.class;
		default:
			return String.class;
		}
	}

	public Agent getSelectedAgent(){
		for(int i=0;i<selectedTab.length;i++){
			if(selectedTab[i]){
				return agents.get(i);
			}
		}
		return null;
	}
}
